<?php

namespace app\index\model;
use think\Model;
use think\Db;

class vo_video_manage extends Model{
    protected $table = 'vo_video_manage';

    //获取当前分类云视频的总数
    public function get_count($classId)
    {
        return $this->where(['classId' => $classId, 'isLine' => 0])->count();
    }

    //根据fileId获取视频
    public function get_video($classId, $fileId)
    {
        return $this->field('fileId, fileUrl, pic, fileName, play, love, duration, isFree, dur_free')
            ->where(['classId' => $classId, 'isLine' => 0, 'fileId' => $fileId])
            ->select();
    }

    //按照上传时间 获取除当前分类 fileId 以外的另外4个随机视频
    public function get_video_other($classId, $fileId, $limit, $next=0)
    {
        $data = Db::table($this->table)
            ->field('fileId, fileUrl, pic, fileName, play, hotNum, love, duration, isFree, dur_free')
            ->where(['classId' => $classId, 'isLine' => 0, 'fileId' => ['<>', $fileId]])
            ->where(function ($query){
                $query->where('show_all', 0)->whereOr('show', 1);
            })
            ->order('updateTime', 'desc')
            ->limit($next,$limit)
            ->select();

        return $data;
    }

    //按照上传时间 获取$limit个视频
    public function get_videos($classId, $limit, $next=0)
    {
        $data = Db::table($this->table)
            ->field('fileId, fileUrl, pic, fileName, play, hotNum, love, duration, isFree, dur_free')
            ->where(['classId' => $classId, 'isLine' => 0])
            ->where(function ($query){
                $query->where('show_all', 0)->whereOr('show', 1);
            })
            ->order('updateTime', 'desc')
            ->limit($next,$limit)
            ->select();

        return $data;
    }

    //换一换 随机获取$limit个随机视频
    public function get_videos_rand($classId, $limit, $next=0)
    {
        $subQuery = Db::table($this->table)
            ->field('fileId, fileUrl, pic, fileName, play, hotNum, love')
            ->where(['classId' => $classId, 'isLine' => 0])
            ->where(function ($query){
                $query->where('show_all', 0)->whereOr('show', 1);
            })
            ->orderRaw('rand()')
            ->limit($next,$limit)
            ->buildSql();

        return Db::table($subQuery.' a')->order('a.hotNum', 'desc')->select();
    }

    //小程序获取$limit个视频
    public function get_videos_applet($classId, $limit, $page=1)
    {
        $start = ($page-1)*$limit;

        $data = $this->where(['classId' => $classId, 'isLine' => 0, 'hotNum' => 99])->find();
        if($data){
            return $this->field('fileId, fileUrl, pic, fileName, play')
                ->where(['classId' => $classId, 'isLine' => 0, 'hotNum' => 99])
                ->order('updateTime', 'desc')
                ->limit($start, $limit)
                ->select();
        }

        return $this->field('fileId, fileUrl, pic, fileName, play')
            ->where(['classId' => $classId, 'isLine' => 0])
            ->where(function ($query){
                $query->where('show_all', 0)->whereOr('show', 2);
            })
            ->order('updateTime', 'desc')
            ->limit($start, $limit)
            ->select();
    }

    //h5版本获取$limit个视频
    public function get_videos_h5($where, $limit, $page=1)
    {
        $start = ($page-1)*$limit;

        return $this->where($where)
            ->where(function ($query){
                $query->where('show_all', 0)->whereOr('show', 3);
            })
            ->order('updateTime', 'desc')
            ->limit($start, $limit)
            ->select();
    }

    //按照热力值大于等于100 获取 推荐视频
    public function get_videos_local($limit, $next=0)
    {
        $subQuery = Db::table($this->table)
            ->field('fileId, fileUrl, classId, pic, fileName, play, hotNum, duration')
            ->where(['isLine' => 0, 'hotNum' => ['>=', 100]])
            ->where(function ($query){
                $query->where('show_all', 0)->whereOr('show', 1);
            })
            ->orderRaw('rand()')
            ->limit($next,$limit)
            ->buildSql();

        return Db::table($subQuery.' a')->order('a.hotNum', 'desc')->select();
    }

    //根据热力值去获取视频
    public function get_hot($hotNum)
    {
        return $this->field('id')->where(['hotNum' => $hotNum, 'isLine' => 0])->select();
    }

    //导航版本获取推荐视频(热力值120的位置固定)
    public function get_vod_suggest()
    {
        $d1 = $this->where(['hotNum' => 120, 'isLine' => 0])->limit(1)->cache(true, 60)->select();

        $map = ['isLine' => 0, 'hotNum' => ['>=', 100]];
        if($d1){
            $limit = 6;
            $map['fileId'] = ['<>', $d1[0]['fileId']];
        } else {
            $limit = 7;
        }

        $subQuery = Db::table($this->table)
            ->where($map)
            ->where(function ($query){
                $query->where('show_all', 0)->whereOr('show', 1);
            })
            ->orderRaw('rand()')
            ->limit(0, $limit)
            ->buildSql();

        $d2 = Db::table($subQuery.' a')->order('a.hotNum', 'desc')->cache(true, 60)->select();

        return array_merge($d1, $d2);
    }
}